package com.koo.modules.sale.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koo.common.utils.MyStrUtil;
import com.koo.common.utils.PageUtils;
import com.koo.common.utils.Query;
import com.koo.framework.feign.SystemFeignService;
import com.koo.modules.purchase.entity.vo.PurchaseQueryVo;
import com.koo.modules.sale.dao.SalePrescriptionOrderDao;
import com.koo.modules.sale.entity.SalePrescriptioinOrderDetailEntity;
import com.koo.modules.sale.entity.SaleAdjustPriceEntity;
import com.koo.modules.sale.entity.SalePrescriptionOrderEntity;
import com.koo.modules.sale.service.SalePrescriptioinOrderDetailService;
import com.koo.modules.sale.service.SalePrescriptionOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class SalePrescriptionOrderServiceImpl extends ServiceImpl<SalePrescriptionOrderDao, SalePrescriptionOrderEntity> implements SalePrescriptionOrderService {

    @Autowired
    private SalePrescriptioinOrderDetailService prescriptioinOrderDetailService;
    @Autowired
    private SystemFeignService systemFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<SalePrescriptionOrderEntity> wrapper = new QueryWrapper<>();
        List<Long> ids = new ArrayList<>();
        boolean isItemQuery = false;

        Object dataForm = params.get("dataForm");
        if (dataForm != null) {
            PurchaseQueryVo queryVo = JSON.parseObject(dataForm + "", PurchaseQueryVo.class);
            String queryDates = queryVo.getQueryDates();
            Integer auditStatus = queryVo.getAuditStatus();
            String key = queryVo.getKey();
            String queryField = queryVo.getQueryField();
            String itemKey = queryVo.getItemKey();
            String queryItemField = queryVo.getQueryItemField();

            if (queryVo != null) {
                //子查询，获取明细ID
                if (MyStrUtil.isNotEmpty(itemKey) && MyStrUtil.isNotEmpty(queryItemField)) {
                    isItemQuery = true;
                    QueryWrapper<SalePrescriptioinOrderDetailEntity> itemWrapper = new QueryWrapper<>();
                    itemWrapper.like(queryItemField, itemKey);
                    List<SalePrescriptioinOrderDetailEntity> list = prescriptioinOrderDetailService.list(itemWrapper);
                    if (CollectionUtils.isNotEmpty(list)) {
                        for (SalePrescriptioinOrderDetailEntity detailEntity : list) {
                            ids.add(detailEntity.getPrescriptionId());
                        }
                    }
                }
                if (MyStrUtil.isNotEmpty(queryDates)) {
                    String[] queryDateArr = queryDates.split(",");
                    String startDate = queryDateArr[0];
                    String endDate = queryDateArr[1];
                    wrapper.ge(MyStrUtil.isNotEmpty(startDate), "prescription_date", startDate);
                    wrapper.le(MyStrUtil.isNotEmpty(endDate), "prescription_date", endDate);
                }
                wrapper.eq(auditStatus != null, "IFNULL(audit_status, 0)", auditStatus);
                wrapper.like(StringUtils.isNotBlank(queryField) && StringUtils.isNotBlank(key), queryField, key);
            }
        }

        IPage<SalePrescriptionOrderEntity> page = new Query<SalePrescriptionOrderEntity>().getPage(params);
        if (isItemQuery) {
            if(CollectionUtils.isNotEmpty(ids)){
                page = baseMapper.listPage(page, wrapper, ids);
            }
        } else {
            page = baseMapper.selectPage(page, wrapper);
        }
        return new PageUtils(page);
    }

    @Override
    public List<SalePrescriptionOrderEntity> listByStoreId(Long storeId) {
        LambdaQueryWrapper<SalePrescriptionOrderEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MyStrUtil.isNotEmpty(storeId), SalePrescriptionOrderEntity::getStoreId, storeId);
        return this.list(wrapper);
    }
}